Separate compilation.md (1504B)
1 +++ 2 title = 'Separate compilation' 3 +++ 4 # Separate compilation 5 as soon as project grows to multiple files, compilation must be managed 6 7 files involved depend on one another, updating one causes others to be recompiled 8 9 ## make 10 - the first Unix tool to automate compile-and-build process 11 - reads a makefile, which describes dependencies 12 - also reads timestamps to determine which file is newest 13 - only re-compile the stuff that depends on a newer file 14 15 ### makefile entries 16 syntax is as follows: 17 18 ``` 19 target: *list of sources* 20 command of recipe steps (compilation) 21 ``` 22 23 has to be indented with a single tab, unless you set `.RECIPEPREFIX` like in [this article](https://tech.davis-hansson.com/p/make/). 24 25 can set variables and use them like in bash, for example for compiler or flags 26 27 wildcard: % (example: %.o) 28 29 automatic variables: 30 31 - $@ — filename of the target 32 - $< — filename of first prerequisite 33 34 ### phony targets 35 `.PHONY`: can declare a phony target so that the target is run even if a file of the same name exists in the directory. 36 37 e.g.: 38 39 ```make 40 .PHONY: clean 41 clean: 42 rm *.o temp 43 ``` 44 45 ### example: 46 47 ```make 48 CC=g++ 49 CFLAGS= -std=c++11 -stdlib=libc++ -Weverything 50 51 all: bank 52 53 account.o: account.h account.cpp 54 $(CC) $(CFLAGS) -c account.cpp 55 56 bank.o: bank.cpp bank.h account.o 57 $(CC) $(CFLAGS) -c bank.cpp 58 59 main.o: main.cpp bank.o 60 $(CC) $(CFLAGS) -c main.cpp 61 62 bank: bank.o main.o 63 $(CC) $(CFLAGS) main.o bank.o account.o -o bank 64 65 clean: 66 rm *.o bank 67 ```